<template>
  <div class="login-container">
    <h2 class="login-title">电子点名册</h2>
    <a-form ref="form" :model="form" id="login-form" :ref="loginForm">
      <h2 class="title">用户登录</h2>
      <a-form-item>
        <a-input class="inputBox" v-model:value="form.username">
          <a-icon slot="prefix" type="user" style="color:rgba(0,0,0,.25)" />
        </a-input>
      </a-form-item>
      <a-form-item>
        <a-input-password class="inputBox" v-model:value="form.password">
          <a-icon slot="prefix" type="lock" style="color:rgba(0,0,0,.25)" />
        </a-input-password>
      </a-form-item>

      <a-form-item>
        <a-button class="submit" type="primary" @click="login">登录</a-button>
      </a-form-item>
    </a-form>
  </div>

</template>

<script>
import axios from 'axios'
import store from '../store'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      userToken: '',
    }
  },
  methods: {
     login()  {
      let _this = this;
      console.log("#### this.form ####");
      console.log(this.form);
      if (this.form.username === '' || this.form.password === '') {
        alert('账号或密码不能为空');
      } else {
        axios({
          method: 'post',
          url: 'http://127.0.0.1:8080/login/',
          data: {
              "user_name":this.form.username,
              "user_pwd":this.form.password
          }
        }).then(res => {
          console.log(res.data);
          this.userToken = res.data.token;
          // 将用户token保存到vuex中
          store.token = this.userToken;
          //this.$router.push('/home');
          alert('登陆成功');
          //获取回跳的redirect地址
          const redirect = this.$route.query.redirect
          if (redirect) {
            //如果redirect存在说明当前用户是进入某页面后未登陆自动跳转到登陆页面来的，所以登陆完成后得再次回跳到该地址
            this.$router.push(redirect)
          } else {
            //否则跳转到默认的页面，首页或者其他页面
            this.$router.push('/')
          }
        }).catch(error => {
          alert('账号或密码错误');
          console.log(error);
        });
      }
    },
  },
}
</script>

<style>
#login-form {
  width: 565px;
  height: 372px;
  margin: 0 auto;
  background: url("../assets/backgroundpic.png");
  padding: 40px 110px;
  text-align: center;
}

/* 背景 */
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../assets/backgroundpic.png");
  text-align: center;
}

/* Log */
.login-title {
  color: #fff;
  text-align: center;
  margin: 150px 0;
  font-size: 48px;
  font-family: Microsoft Yahei;
}
/* 登陆按钮 */
.submit{
  width: 100%;
  height: 45px;
  font-size: 16px;
}
/* 用户登陆标题 */
.title{
  margin-bottom: 50px;
  color: #fff;
  font-weight: 700;
  font-size: 24px;
  font-family: Microsoft Yahei;
}
/* 输入框 */
.inputBox{
  height: 45px;
}
/* 输入框内左边距50px */
.ant-input-affix-wrapper .ant-input:not(:first-child) {
    padding-left: 50px;
}
</style>
